package com.suray.wcs.web.system.controller.dict;

import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.core.utils.ExcelUtil;
import com.suray.commin.core.utils.SecurityUtils;
import com.suray.commin.core.utils.StringUtils;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.system.entity.SysDictData;
import com.suray.wcs.service.system.entity.SysDictType;
import com.suray.wcs.service.system.service.ISysDictDataService;
import com.suray.wcs.service.system.service.ISysDictTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author shaozn
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends BaseController {
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation(value = "查询字典数据列表")
    @PreAuthorize("@ss.hasPermi('dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    /**
     * 查询字典数据详细
     */
    @ApiOperation(value = "查询字典数据详细")
    @GetMapping(value = "/{dictCode}")
    public ResponseData getInfo(@PathVariable Long dictCode) {
        return ResponseData.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation(value = "根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public ResponseData dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return ResponseData.success(data);
    }

    /**
     * 新增字典类型
     */
    @ApiOperation(value = "新增字典类型")
    @PreAuthorize("@ss.hasPermi('dict:add')")
    @Log(title = "字典数据", method = "新增字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseData add(@Validated @RequestBody SysDictData dict) {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation(value = "修改保存字典类型")
    @PreAuthorize("@ss.hasPermi('dict:edit')")
    @Log(title = "字典数据", method = "修改保存字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseData edit(@Validated @RequestBody SysDictData dict) {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @ApiOperation(value = "删除字典类型")
    @PreAuthorize("@ss.hasPermi('dict:remove')")
    @Log(title = "字典类型", method = "删除字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public ResponseData remove(@PathVariable Long[] dictCodes) {
        return toAjax(dictDataService.deleteDictDataByIds(dictCodes));
    }

    @Log(title = "字典类型", method = "导出字典类型", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('dict:export')")
    @GetMapping("/export")
    public ResponseData export(SysDictData dictData) {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        return util.exportExcel(list, "字典数据");
    }
}
